<?php
/**
 * FinancialService.php
 * ==============================================
 * Copy right 2015-2021  by https://www.tianmtech.com/
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @desc : 财务系统服务
 * @author: zhanglinxiao<zhanglinxiao@tianmtech.cn>
 * @date: 2022/01/07
 * @version: v1.0.0
 * @since: 2022/01/07 09:11
 */

namespace App\Services\Tools;


class ContentService
{
    private $host; //域名

    private $appKey; //app_key

    private $appSecret;    //app_secret

    private $timeout;   //请求接口超时设置

    public function __construct()
    {
        $this->host = env("CONTENT_HOST");
        $this->appKey = env("CONTENT_APP_ID");
        $this->appSecret = env("CONTENT_APP_SECRET");
        $this->timeout = env("CONTENT_TIMEOUT");

    }



    /**
     * 生成签名
     * @param $params
     * @param $secret
     * @return string
     */
    public function generateSign($params)
    {
        $keys = array_keys($params);
        arsort($keys);
        $str = '';
        foreach ($keys as $key) {
            $str .= $key . $params[$key];
        }

        $str .= $this->appSecret;
        $sig = md5($str);
        return $sig;
    }

    /**
     * 获取过期时间
     * @return int
     */
    public function getTimeout()
    {
        $timeout = $this->timeout;
        if (empty($timeout)) {
            $timeout = 30;
        }
        return $timeout;
    }

    /**
     * 获取请求头信息
     * @param $params
     * @return array
     */
    private function getHeader($params)
    {
        $timestamp = time();

        $params['timestamp'] = $timestamp;
        $params['appkey'] = $this->appKey;
        $sign = $this->generateSign($params);

        $header = array();
        $header[] = "timestamp:" . $timestamp;
        $header[] = "sign:" . $sign;
        $header[] = "appkey:" . $this->appKey;
        return $header;
    }

    /**
     * 请求
     * @param $path
     * @param $method
     * @param $params
     * @return array|bool
     */
    private function request($path, $method, array $params)
    {
        $result = false;

        $url = $this->host . $path;
        $header = $this->getHeader($params);
        $header["Content-Type"] = "application/json";
        try {
            if (strtolower($method) == 'post') {
                $req_result = $this->sendPost($url, $params, $header);
            } else {
                $req_result = $this->sendGet($url, $params, $header);
            }
            if (!$req_result) {
                return false;
            }
            if (!empty($req_result) && is_string($req_result)) {
                $req_result = json_decode($req_result, true);
                if (!empty($req_result) && is_array($req_result)) {
                    $result = $req_result;
                }
            }

        } catch (\Exception $e) {
            $result = false;
        }
        return $result;
    }

    /**
     * 发送post 请求
     * @param $url
     * @param $param
     * @param array $header
     * @return bool|string
     */
    private function sendPost($url, $param, $header = [])
    {
        $cl = curl_init();//①：初始化
        curl_setopt($cl, CURLOPT_URL, $url);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
        if (!empty($header)) {
            curl_setopt($cl, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($cl, CURLOPT_POST, 1);
        curl_setopt($cl, CURLOPT_POSTFIELDS, $param);

        $content = curl_exec($cl);//③：执行并获取结果
        $code = curl_getinfo($cl, CURLINFO_HTTP_CODE);
        if ($code != 200) return false;
        curl_close($cl);//④：释放句柄
        return $content;
    }

    /**
     * get 请求
     * @param $url
     * @param $params
     * @param array $header
     * @return bool|string
     */
    private function sendGet($url, $params, $header = [])
    {
        //构造请求参数
        $param = http_build_query($params);
        $url = $url . '?' . $param;
        //初始化
        $ch = curl_init();
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($code != 200) return false;
        curl_close($ch);
        return $content;
    }

    /**
     * 获取用户处罚列表
     * @param $params
     * @return array
     */
    public function getPunishList($params)
    {
        $path = "/api/punishs";
        $result = $this->request($path, 'get', $params);
        return $result;
    }

    /**
     * 获取用户处罚选项列表
     * @param $type int 类型 1原因 2时间
     * @return array
     */
    public function getPunishOption($type = 1)
    {
        $path = "/api/punish/reason/option";
        if ($type==1) {
            $path = "/api/punish/reason/option";
        } else {
            $path = "/api/punish/forbid/time/option";
        }

        $result = $this->request($path, 'get',[]);
        return $result;
    }

    /**
     * 添加封禁记录
     * @param $params
     * @return array|bool
     */
    public function addPunishRecord($params)
    {
        $path = "/api/punish/record";
        $data = [
            'user_id'           => $params['user_id'],
            'punish_id'         => 3,
            'punish_reason_id'  => $params['punish_reason_id'],
            'content_info_type' => $params['id'],
            'forbid_time_id'    => $params['forbid_time_id']
        ];
        $result = $this->request($path, 'post',$data);
        return $result;
    }

    /**
     * 添加解封记录
     * @param $params
     * @return array|bool
     */
    public function addPunishBanReliveRecord($params)
    {
        $path = "/api/punish/user/ban/relive/record";
        $data = [
            'user_id'           => $params['user_id'],
            'content_info_type' => $params['id'],
        ];
        $result = $this->request($path, 'post',$data);
        return $result;
    }
}